<?php echo $html->link(__("Search Orders"),"#",array("onclick"=>'$("#search_form").toggle();return false;','class'=>'add_button'));?>
<div id="search_form" style="<?php echo !isset($this->data['Order']) || !$this->data['Order'] ? "display:none" : "" ?>">
<?php
  echo $searchForm->create('Order',array('url'=>"/".$this->params['url']['url'],'class'=>'admin_search order_search')); 
  $fields = cri("Order")->getFields("group","search");
  echo $searchForm->inputs(am(array('fieldset'=>false),$fields));
 ?>
  <div class="input submit">
  <?php
  echo $searchForm->submit(__("Reset"),array("type"=>"submit",'div'=>false,'onclick'=>'$(this).closest("form")[0].reset();$(this).closest("form").find("select").val("");return false;'));
  echo "<span style='margin-left:10px;'></span>";
  echo $searchForm->submit(__("Search"),array("div"=>false));?>
  </div>  
  <div class="clear"></div></form><?php
?>

</div>
<?php
    function showOrderPlanName($record)
    {
      return $record['ListingPlan']['plan_info']['Plan']['title'];
    }
    
    function showOrderListingName($record)
    {
      $listings = ClassRegistry::getObject('View')->viewVars['listings'];
      $listing = $listings[$record['ListingPlan']['listing_id']];
      return $listing;
    }
	$html->css("/paid_listings/css/style.css",array(),null,false);
	$OrderModel = cri("PaidListings.Order");
    foreach($orders as $key => $order)
    {
      if(!$order['Order']['payment_handler'])
      {
  //      $orders[$key]['Order']['payment_handler'] = __("N/A");
      }
      if($order['Order']['start_date'])
      {
        $order['Order']['start_date'] = date('Y-m-d',strtotime($order['Order']['start_date']));
      }
      if($order['Order']['start_date'] == '1970-01-01' || !$order['Order']['start_date'])
      {
        $order['Order']['start_date'] = __("N/A");
      }
      $orders[$key]['Order']['start_date'] = $order['Order']['start_date'];
      if(!$order['Order']['end_date'] && $order['ListingPlan']['status'] == ListingPlan::STATUS_ACTIVE)
      {
        $orders[$key]['Order']['end_date']  = __("Never Expire");
      }else if($order['Order']['end_date'])
      {
        $orders[$key]['Order']['end_date'] = date('Y-m-d',strtotime($order['Order']['end_date']));
      }else
      {
        $orders[$key]['Order']['end_date'] = __("N/A");
      }
      $orders[$key]['Order']['total_amount'] = $order['Order']['currency_symbol'].$orders[$key]['Order']['total_amount'].' ('.$orders[$key]['Order']['currency'].')';
      $orders[$key]['ListingPlan']['user'] = __("N/A");
      if(isset($order['ListingPlan']['poster']))
      {
        $orders[$key]['ListingPlan']['user'] = $order['ListingPlan']['poster']['username'];
      }
    }
    $index->setData($orders,'Order');
    $index->showFields(array("id",'plan_name','listing','status','ListingPlan.user','ListingPlan.status','Order.payment_type','total_amount',array('payment_handler'=>__("Payment method")),array('start_date'=>__("Activation date")),array('end_date'=>__("Expiration date"))));
    $index->sortablefields(array("id","status","ListingPlan.status",'Order.payment_type','start_date','end_date'));
    $index->addColumn('plan_name','showOrderPlanName');
    $index->addColumn('listing','showOrderListingName');
    $index->setAction(__("View"),'/admin/paid_listings/orders/view/:Order.id');
    $index-> setAction(__("Edit"),'/admin/paid_listings/orders/edit/:Order.id');  
    //$index->addColumn('plan_name','showOrderPlanName');
    echo $index->display();        
 ?>